interface coustomEvent {
    on:(name:string,fn:Function) => void
    emit:(name:string,...args:Array<any>) => void
    off:(name:string,fn:Function) => void
    once:(name:string,fn:Function) => void
}
interface List {
    [key:string]:Array<Function>
}
class dispatch implements coustomEvent {
    list:List
    constructor() {
        this.list = {}
    }
    on(name:string,fn:Function){
        const callback = this.list[name] || []
        callback.push(fn)
        this.list[name] = callback
    }
    emit(name:string,...args:Array<any>){
        let eventName = this.list[name]
        if (eventName){
            eventName.forEach(fn => {
                fn.apply(this,args)
            })
        }else{
            console.error(`名称错误${name}`)
        }
    }
    off(name:string,fn:Function){
        let eventName = this.list[name]
        if (eventName && fn){
            let index = eventName.findIndex(fns => fns === fn)
            eventName.splice(index,1)
        }else{
            console.error(`名称错误${name}`)
        }
    }
    once(name:string,fn:Function){
        let de = (...args:Array<any>) => {
            fn.apply(this,args)
            this.off(name,de)
        }
        this.on(name,de)
    }
}
const dispatchEx = new dispatch()
const fnv = (...args:Array<any>) => console.log(args,2)
//添加一个事件(监听事件)
dispatchEx.on('post',fnv)
dispatchEx.on('post',fnv)
//关闭一个事件
// o.off('post',fnv)
//只执行一次
dispatchEx.once('post',fnv)
dispatchEx.emit('post',1,false,{name:'小周'})

